package com.wealth.commodity.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.service.ICommentService;
import com.wealth.system.web.controller.BaseController;

import iaas.utils.ResultObject;

/**
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/comment")
public class CommentController extends BaseController{

	Logger logger = Logger.getLogger(CommentController.class);
	
	@Autowired
	private ICommentService commentService;
	
	/** 
	 * 商户所有商品评论列表 ${path}/comment?merchantid=1
	 * 商品评论列表	${path}/comment?goodsid=1
	 */
	@RequestMapping
	public String listForm(Model model,HttpServletRequest request) {
		try{
			if(request.getParameter("merchantid")!=null && request.getParameter("merchantid")!="" ){
				Long merchantid = Long.valueOf(request.getParameter("merchantid"));
				Integer avgScore = commentService.getAvgScoreByMerchantId(merchantid);
				Integer commentNum = commentService.getCommentNumByMerchantId(merchantid);
				model.addAttribute("avgScore",avgScore);
				model.addAttribute("commentNum",commentNum);
				model.addAttribute("merchantid",merchantid);
			}
			//商品评论列表 
			if(request.getParameter("goodsid")!=null && request.getParameter("goodsid")!=""){
				Long goodsid = Long.valueOf(request.getParameter("goodsid"));
				Integer avgScore = commentService.getAvgScore(goodsid);
				Integer commentNum = commentService.getCommentNum(goodsid);
				model.addAttribute("avgScore",avgScore);
				model.addAttribute("commentNum",commentNum);
				model.addAttribute("goodsid",goodsid);
			}
			return "commodity/comment/list";
		}catch(Exception e){
			return "system/500";
		}
	}

	/**
	 * 加载商户下 商品评论
	 */
	@RequestMapping(value="/more",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject list(
			@RequestParam(defaultValue="-1") Long merchantid,
			@RequestParam(defaultValue="-1") Long goodsid,
			@RequestParam(defaultValue="-1") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			if(goodsid==-1){
				List<Comment> orderList = commentService.ajaxCommentByMerchantId(merchantid, index, offset);
				return new ResultObject("000", "加载成功", orderList);
			}else{
				List<Comment> orderList = commentService.ajaxComment(goodsid, index, offset);
				return new ResultObject("000", "加载成功", orderList);
			}
		}catch(Exception e){
			return new ResultObject("999", "加载错误", "");
		}
	}

	
}
